package com.kxmall.web.controller.newtimes.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kxmall.common.core.domain.PageQuery;
import com.kxmall.common.core.page.TableDataInfo;
import com.kxmall.common.utils.StringUtils;
import com.kxmall.group.domain.KxGroupShop;
import com.kxmall.newtimes.domain.KxNewTimes;
import com.kxmall.newtimes.domain.bo.KxNewTimesBo;
import com.kxmall.newtimes.domain.vo.KxNewTimesVo;
import com.kxmall.newtimes.mapper.KxNewTimesMapper;
import com.kxmall.web.controller.newtimes.service.IKxNewTimesService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 新鲜时报Service业务层处理
 *
 * @author kxmall
 * @date 2023-10-05
 */
@RequiredArgsConstructor
@Service
public class KxNewTimesServiceImpl implements IKxNewTimesService {

    private final KxNewTimesMapper baseMapper;

    /**
     * 查询新鲜时报
     */
    @Override
    public KxNewTimesVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询新鲜时报列表
     */
    @Override
    public TableDataInfo<KxNewTimesVo> queryPageList(KxNewTimesBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<KxNewTimes> lqw = buildQueryWrapper(bo);
        Page<KxNewTimesVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询新鲜时报列表
     */
    @Override
    public List<KxNewTimesVo> queryList(KxNewTimesBo bo) {
        LambdaQueryWrapper<KxNewTimes> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<KxNewTimes> buildQueryWrapper(KxNewTimesBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<KxNewTimes> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getStorageId() != null, KxNewTimes::getStorageId, bo.getStorageId());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), KxNewTimes::getContent, bo.getContent());
        lqw.eq(bo.getIsStop() != null, KxNewTimes::getIsStop, bo.getIsStop());
        lqw.in(!CollectionUtils.isEmpty(bo.getStorageIds()), KxNewTimes::getStorageId, bo.getStorageIds());
        return lqw;
    }

    /**
     * 新增新鲜时报
     */
    @Override
    public Boolean insertByBo(KxNewTimesBo bo) {
        KxNewTimes add = BeanUtil.toBean(bo, KxNewTimes.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改新鲜时报
     */
    @Override
    public Boolean updateByBo(KxNewTimesBo bo) {
        KxNewTimes update = BeanUtil.toBean(bo, KxNewTimes.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(KxNewTimes entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除新鲜时报
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
